﻿using EmbeddedSensorCloud.HTTP;
using System;
using System.Windows.Forms;
using WindowsInput;
using WindowsInput.Native;

namespace EmbeddedSensorCloud.Plugin.Custom
{
    public sealed class Program : IPlugin
    {
        private InputSimulator simulator;
        private string body;

        public bool IsResponseable(Request request)
        {
            return request.Url.StartsWith("/custom");
        }

        public Response Respond(Request request)
        {
            VirtualKeyCode key;
            if (request.Args.ContainsKey("key") && Enum.TryParse<VirtualKeyCode>(request.Args["key"], out key))
            {
                this.simulator.Keyboard.KeyDown(key);
            }

            return new Response(body);
        }

        public void Initialize()
        {
            this.simulator = new InputSimulator();
            this.body = Properties.Resources.index;

            string buttonStr = "";
            foreach (var name in Enum.GetNames(typeof(VirtualKeyCode)))
            {
                buttonStr += string.Format("<input type=\"button\" value=\"{0}\" onclick=\"clickBut('{0}')\" />", name);
            }

            this.body = this.body.Replace("@@data@@", buttonStr);
        }
    }
}
